<?php

declare(strict_types=1);

use think\migration\Migrator;

class CreateSysTables extends Migrator
{
    public function change(): void
    {
        $this->createSysConfigTable();
        $this->createSysMenuTable();
        $this->createSysLogTable();
    }

    private function createSysConfigTable(): void
    {
        $this->table('sys_config', [
            'comment' => '系统配置',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('config_key', 'string', [
                'limit' => 150,
                'comment' => '配置键',
            ])
            ->addColumn('value_type', 'string', [
                'limit' => 20,
                'default' => 'string',
                'comment' => '值类型 string/int/bool/json',
            ])
            ->addColumn('config_value', 'text', [
                'comment' => '配置值',
            ])
            ->addColumn('description', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '描述',
            ])
            ->addColumn('category', 'string', [
                'limit' => 50,
                'default' => 'default',
                'comment' => '分类',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-禁用 1-启用',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['config_key'], ['unique' => true])
            ->addIndex(['category'])
            ->create();
    }

    private function createSysMenuTable(): void
    {
        $this->table('sys_menu', [
            'comment' => '系统菜单/按钮',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('parent_id', 'integer', [
                'default' => 0,
                'comment' => '父菜单ID',
            ])
            ->addColumn('type', 'string', [
                'limit' => 20,
                'default' => 'menu',
                'comment' => '节点类型 menu/button',
            ])
            ->addColumn('name', 'string', [
                'limit' => 120,
                'comment' => '菜单名称',
            ])
            ->addColumn('identifier', 'string', [
                'limit' => 120,
                'comment' => '菜单标识',
            ])
            ->addColumn('path', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '路由路径',
            ])
            ->addColumn('component', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '前端组件',
            ])
            ->addColumn('redirect', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '重定向路径',
            ])
            ->addColumn('meta', 'json', [
                'null' => true,
                'comment' => '元信息',
            ])
            ->addColumn('permission_code', 'string', [
                'limit' => 150,
                'null' => true,
                'comment' => '关联权限编码',
            ])
            ->addColumn('client_type', 'string', [
                'limit' => 20,
                'default' => 'admin',
                'comment' => '客户端 admin/seller/support',
            ])
            ->addColumn('sort', 'integer', [
                'default' => 0,
                'comment' => '排序',
            ])
            ->addColumn('is_visible', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '是否展示',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-禁用 1-启用',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['identifier'], ['unique' => true])
            ->addIndex(['client_type'])
            ->addIndex(['permission_code'])
            ->create();
    }

    private function createSysLogTable(): void
    {
        $this->table('sys_operation_log', [
            'comment' => '系统操作日志',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '操作人ID',
            ])
            ->addColumn('username', 'string', [
                'limit' => 60,
                'default' => '',
                'comment' => '操作人名称快照',
            ])
            ->addColumn('action', 'string', [
                'limit' => 120,
                'comment' => '操作行为',
            ])
            ->addColumn('method', 'string', [
                'limit' => 10,
                'default' => '',
                'comment' => 'HTTP方法',
            ])
            ->addColumn('path', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '请求路径',
            ])
            ->addColumn('params', 'json', [
                'null' => true,
                'comment' => '请求参数',
            ])
            ->addColumn('ip', 'string', [
                'limit' => 45,
                'default' => '',
                'comment' => 'IP地址',
            ])
            ->addColumn('user_agent', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => 'UA',
            ])
            ->addColumn('status_code', 'integer', [
                'default' => 200,
                'comment' => '响应码',
            ])
            ->addColumn('message', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注信息',
            ])
            ->addColumn('created_at', 'timestamp', [
                'default' => 'CURRENT_TIMESTAMP',
                'comment' => '操作时间',
            ])
            ->addIndex(['user_id'])
            ->addIndex(['created_at'])
            ->create();
    }
}
